use "scdsdata_FM.dta", clear

***Table 1 sample distribution***
preserve
bysort country1 firm: gen unique_firm = (_n == 1)
bysort country1: gen n_obs = _N

* Collapse using sum for both
collapse (sum) n_unique_firms=unique_firm (mean) n_obs=n_obs, by(country1)

list country1 n_obs n_unique_firms, clean

restore

preserve
duplicates drop country1 year_cds, force
list country1 year_cds, clean
restore

***Table 2***
quietly estpost sum budget cci cpi enforce epu lag_fixcapgro fx_hedge lag_gdpgro govexp lag_inflation netcredit pcr pcredit qog lag_tfpgro totcredit transparency unemployment vix yield, detail 
esttab using "table2.rtf",replace cells("mean(fmt(%5.3f)) sd(fmt(%5.3f)) p25(fmt(%5.3f)) p50(fmt(%5.3f)) p75(fmt(%5.3f)) count(fmt(%5.0f))") noobs ///
title({\b Table2.} {\i Descriptive statistics})
est clear

***Table 3***
eststo: quietly reghdfe capex post, a(country1 year) vce(cluster firm)
eststo: quietly reghdfe capex post, a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash , a(country1 year firm ff_industry) vce(cluster firm)
esttab using table3.rtf, replace b(4) se(4) ar2(4)star(* 0.10 ** 0.05 *** 0.01) nogaps
eststo clear

***Figure 1***

use "aggregate.dta", clear

***credit to private setcor without ITA & RUS****
preserve
collapse (mean) credit if window<4 & window>-2 & countrycode!="ITA" & countrycode!="RUS" , by(window)
twoway line credit window, ///
    xlabel(-1(1)3) /// 
    title("Domestic Credit to Private Sector") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")
restore
 

***net domestic credit without ITA & RUS******** 
 preserve
collapse (mean) netcredit if window<4 & window>-2 & countrycode!="ITA" & countrycode!="RUS" , by(window)
twoway line netcredit window, ///
    xlabel(-1(1)3) /// 
    title("Net Domestic Credit") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")
restore

preserve
collapse (mean) totcredit if window<4 & window>-2 , by(window)
twoway line totcredit window, ///
    xlabel(-1(1)3) /// 
    title("Total Credit to Private Non-Financial Sector") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")	
restore

preserve
collapse (mean) gross_fixed_capital if window<4 & window>-2 , by(window)
twoway line gross_fixed_capital window, ///
    xlabel(-1(1)3) /// 
    title("Gross fixed capital formation") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")	
restore

preserve
collapse (mean) gross_capital if window<4 & window>-2 , by(window)
twoway line gross_capital window, ///
    xlabel(-1(1)3) /// 
    title("Gross capital formation") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")	
restore

***Figure 2***
use "scdsdata_FM.dta", clear
preserve
collapse (mean) capex if window<6 & window>-6 , by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_G8") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")	
restore

preserve
collapse (mean) capex if window<6 & window>-6 & countrycode=="CAN", by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_CAN") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")	
restore

preserve
collapse (mean) capex if window<6 & window>-6 & countrycode=="FRA", by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_FRA") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")		
restore

preserve	
collapse (mean) capex if window<6 & window>-6 & countrycode=="DEU", by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_DEU") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")		
restore	

preserve
collapse (mean) capex if window<6 & window>-6 & countrycode=="ITA", by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_ITA") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")	
restore

preserve
collapse (mean) capex if window<6 & window>-6 & countrycode=="JPN", by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_JPN") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")		
restore

preserve	
collapse (mean) capex if window<6 & window>-6 & countrycode=="RUS", by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_RUS") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")		
restore

preserve
collapse (mean) capex if window<6 & window>-6 & countrycode=="GBR", by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_GBR") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")		
restore

preserve	
collapse (mean) capex if window<6 & window>-6 & countrycode=="USA", by(window)
twoway line capex window, ///
    xlabel(-5(1)5) /// 
    title("Mean Capital Expenditures_USA") ///
    xtitle("Relative to the Launch Year") ///
    ytitle("%")		
restore

***Table 4***
*Panel A
eststo: quietly reghdfe capex c.post#c.totcredit post lag_q lag_cf lag_tdta lag_cash, a(country1 year firm ff_industry ) vce(cluster firm)
eststo: quietly reghdfe capex c.post#c.pcredit post lag_q lag_cf lag_tdta lag_cash, a(country1 year firm ff_industry ) vce(cluster firm)
eststo: quietly reghdfe capex c.post#c.netcredit post lag_q lag_cf lag_tdta lag_cash, a(country1 year firm ff_industry ) vce(cluster firm)
esttab using table4A.rtf, replace b(4) se(4) ar2(4)star(* 0.10 ** 0.05 *** 0.01) nogaps
eststo clear
*Panel B
eststo: quietly reghdfe capex c.post#c.enforce post lag_q lag_cf lag_tdta lag_cash , a(country1 year firm ff_industry ) vce(cluster firm)
eststo: quietly reghdfe capex c.post#c.qog post lag_q lag_cf lag_tdta lag_cash , a(country1 year firm ff_industry ) vce(cluster firm)
eststo: quietly reghdfe capex c.post#c.legal post lag_q lag_cf lag_tdta lag_cash , a(country1 year firm ff_industry ) vce(cluster firm)
eststo: quietly reghdfe capex c.post#c.cpi post lag_q lag_cf lag_tdta lag_cash , a(country1 year firm ff_industry ) vce(cluster firm)
esttab using table4B.rtf, replace b(4) se(4) ar2(4)star(* 0.10 ** 0.05 *** 0.01) nogaps
eststo clear
*Panel C
eststo: quietly reghdfe capex c.post#c.pcr post lag_q lag_cf lag_tdta lag_cash, a(country1 year firm ff_industry ) vce(cluster firm)
eststo: quietly reghdfe capex c.post#c.budget post lag_q lag_cf lag_tdta lag_cash, a(country1 year firm ff_industry ) vce(cluster firm)
eststo: quietly reghdfe capex c.post#c.transparency post lag_q lag_cf lag_tdta lag_cash, a(country1 year firm ff_industry ) vce(cluster firm)
esttab using table4C.rtf, replace b(4) se(4) ar2(4)star(* 0.10 ** 0.05 *** 0.01) nogaps
eststo clear

***Table 5***
*Panel A
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if fcds_init==1, a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if fcds_init==0, a(country1 year firm ff_industry) vce(cluster firm)
esttab using table5A.rtf, replace b(4) se(4) ar2(4)star(* 0.10 ** 0.05 *** 0.01) nogaps
eststo clear

*Panel B
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if psi==1, a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if psi==0, a(country1 year firm ff_industry) vce(cluster firm)
esttab using table5B.rtf, replace b(4) se(4) ar2(4)star(* 0.10 ** 0.05 *** 0.01) nogaps
eststo clear

*Panel C
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if highkz==1, a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if highkz==0, a(country1 year firm ff_industry) vce(cluster firm)
esttab using table5C.rtf, replace b(4) se(4) ar2(4)star(* 0.10 ** 0.05 *** 0.01) nogaps
eststo clear



***Table 6***
***drop one country****
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if countrycode!="CAN" , a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if countrycode!="FRA" , a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if countrycode!="DEU" , a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if countrycode!="ITA" , a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if countrycode!="JPN" , a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if countrycode!="RUS" , a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if countrycode!="GBR" , a(country1 year firm ff_industry) vce(cluster firm)
eststo: quietly reghdfe capex post lag_q lag_cf lag_tdta lag_cash if countrycode!="USA" , a(country1 year firm ff_industry) vce(cluster firm)
esttab using table6.rtf, replace b(4) se(4) ar2(4)star(* 0.10 ** 0.05 *** 0.01) nogaps
eststo clear
